package org.base.oj.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.base.oj.domain.Role;
import org.base.oj.domain.dto.RoleDTO;
import org.base.oj.domain.RolePermission;
import org.base.oj.mapper.RoleMapper;
import org.base.oj.mapper.RolePermissionMapper;
import org.base.oj.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    @Transactional
    public void saveRolePermissionByUserPid(RoleDTO roleDTO) {
        Role role = new Role();
        role.setName(roleDTO.getName());
        role.setRoleKey(roleDTO.getRoleKey());
        roleMapper.insert(role);
        //保存权限
        if (roleDTO.getPermissions() != null){
            for (String permission : roleDTO.getPermissions()) {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(role.getId());
                rolePermission.setPermission(permission);
                rolePermissionMapper.insert(rolePermission);
            }
        }
    }
}
